package org.social.quartz;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import org.apache.commons.lang3.ArrayUtils;
import org.bean.InterceptInfoDTO;
import org.bean.SourceChatMsg;
import org.constant.NoticeConstant;
import org.constant.RemoteServerType;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.remote.game.RemoteGameServer;
import org.social.db.bean.InterceptInfo;
import org.social.db.bean.Notice;
import org.social.db.cache.InterceptInfoCache;
import org.social.db.cache.NoticeCache;
import org.social.manager.StoreManager;
import org.social.manager.InterceptManager;
import org.social.manager.NoticeManager;
import org.utils.RemoteUtil;
import org.utils.TimeUtil;

import com.sojoys.artifact.manager.RemoteProxyManager;
import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.ArrayUtil;

/**
 * Created by fxf on 2017/9/15.
 */
public class MinuteJob implements Job {

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {


		StoreManager.me().autoRefresh();

		Date nowDate = new Date();
		
		
		checkInterceptInfo(nowDate);

		checkNoticeEndTime(nowDate);
		
		// 系统木人发送全服、全部人
//		RemoteGameServer remoteGameServer = RemoteProxyManager.me().create(RemoteGameServer.class, RemoteServerType.GAME, true);
//		SourceChatMsg msg = new SourceChatMsg(0, "System", ChatType.SYSTEM_CHAT, "系统公告测试:当前时间 -> " + DateUtil.now(), 0);
//		remoteGameServer.notifyChat(msg, new HashSet<>(0));

	}

	/**
	 * 检查拦截信息
	 */
	public void checkInterceptInfo(Date nowDate) {
		List<InterceptInfo> all = InterceptInfoCache.me().getCacheAll();
		int[] array =new int[3];
		for (InterceptInfo interceptInfo : all) {
			Date deadline = interceptInfo.getDeadline();
			if (deadline!=null && nowDate.after(deadline)) {
				ArrayUtils.add(array, interceptInfo.getId());
			}
		}
		
		InterceptManager.removeInterceptInfo(array);
	}

	private void checkNoticeEndTime(Date nowDate){
	    List<Notice> noticeList = NoticeCache.me().loadAll();
		List<Integer> noticeIdList = noticeList.stream()
				.filter(notice -> notice.getEndTime().getTime() <= nowDate.getTime())
				.map(notice -> notice.getId())
				.collect(Collectors.toList());
		NoticeManager.deleteNotice(noticeIdList, false);
	}

}
